package com.fau.om.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.om.action.PublicAction;
import com.fau.om.entity.MedicalAdmin;
import com.fau.om.entity.MedicalOrdersStorage;
import com.fau.om.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 订单查找Action
 * @author kangyj
 * @datetime 2014年11月27日,下午3:41:24
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class OrdersFindAction extends PublicAction implements
		ModelDriven<MedicalOrdersStorage> {

	
	private static final long serialVersionUID = -9088645471834431010L;
	
	private MedicalOrdersStorage ordersStorage = new MedicalOrdersStorage();
	
	/**
	 * @desc 订单查找页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月27日,下午3:43:06
	 */
	@Action(value = "orders_find_init", results = {@Result(name = SUCCESS, location = "orders_find_init.jsp")})
	public String init() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询订单查找
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月27日,下午3:46:10
	 */
	@Action(value = "orders_find_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ordersStorage.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getMaintainManagerService().findStorageByPage(ordersStorage, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单维护-订单查询导出当前页
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月20日,上午11:39:19
	 */
	@Action(value = "order_find_export_current")
	public String currentExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"订单查询表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getMaintainManagerService().exportSearchOrdersCurrent(ids);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @desc 订单维护-订单查询导出所有
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月21日,下午11:51:37
	 */
	@Action(value = "orders_find_export_all")
	public String allExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"订单查询表.xls"));
			response.setContentType("application/vnd.ms-excel");
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			ordersStorage.setHospitalId(admin.getEnterpriseId());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getMaintainManagerService().exportSearchOrdersAll(ordersStorage);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 模糊查询核单人员
	 * @return null
	 * @author liy
	 * @datetime 2014年12月24日,下午5:51:27
	 */
	@Action(value = "orders_find_checkuser")
	public String ordersFindCheckuser(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String key = request.getParameter("key");
		list = this.baseAllService.getMaintainManagerService().findCheckuserByName(key);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 打开订单页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月31日,上午10:28:57
	 */
	@Action(value = "orders_find_open_init", results = {@Result(name = SUCCESS, location = "orders_find_open_init.jsp")})
	public String openInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 转到订单信息页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月31日,上午11:14:45
	 */
	@Action(value = "orders_find_orders_info", results = {@Result(name = SUCCESS, location = "orders_find_orders_info.jsp")})
	public String ordersInfoInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 转到客户信息页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月31日,上午11:14:23
	 */
	@Action(value = "orders_find_customer_info", results = {@Result(name = SUCCESS, location = "orders_find_customer_info.jsp")})
	public String customerInfoInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 查询物流公司集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月31日,下午2:19:40
	 */
	@Action(value = "orders_find_logistics_view")
	public String logisticsView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getMaintainManagerService().findLogisticsList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 根据客户id查询客户号码
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月31日,下午3:31:47
	 */
	@Action(value = "orders_find_tel_view")
	public String telInfoView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getMaintainManagerService().findTelInfoList(ordersStorage.getCustomerId());
		formatJSON(response, list);
		return null;
	}
	
	@Override
	public MedicalOrdersStorage getModel() {
		return ordersStorage;
	}
	
	/**
	 * @desc 查询系统信息
	 * @return
	 * @author zhaoey
	 * @datetime 2015年4月11日,下午1:51:13
	 */
	@Action(value = "orders_find_system")
	public String findSystemList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getMaintainManagerService().findSystemList(ordersStorage.getKeyType());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询订单状态
	 * @return
	 * @author zhaoey
	 * @datetime 2015年4月11日,下午1:51:13
	 */
	@Action(value = "orders_find_order_status")
	public String findOrderStatusList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getMaintainManagerService().findOrderStatusList(ordersStorage.getKeyType());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 我的订单查询初始化
	 * @return
	 * @author zhaoey
	 * @datetime 2015年4月15日,下午1:05:36
	 */
	@Action(value = "my_orders_find_init", results = {@Result(name = SUCCESS, location = "my_orders_find_init.jsp")})
	public String myOrdersFindInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 我的订单查询
	 * @return
	 * @author zhaoey
	 * @datetime 2015年4月15日,下午1:05:36
	 */
	@Action(value = "my_orders_find_view")
	public String myOrdersFindView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ordersStorage.setCheckUserId(admin.getId());
		pageBean = this.baseAllService.getMaintainManagerService().findMyOrdersByPage(ordersStorage, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
}
